package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleConfigService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.wemedia.dtos.CommentStatusManageDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class ApArticleConfigServiceImpl extends ServiceImpl<ApArticleConfigMapper, ApArticleConfig> implements ApArticleConfigService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    /**
     * 上架或者下架
     * @param message
     */
    @Override
    public void downOrLoad(String message) {
        if (StringUtils.isEmpty(message)){
            return;
        }
        Map map = JSON.parseObject(message, Map.class);
        boolean isDown = true;
        if (map.get("enable").equals(1)){
            isDown = false;
        }
        update(Wrappers.<ApArticleConfig>lambdaUpdate()
                .eq(ApArticleConfig::getArticleId,map.get("articleId"))
                .set(ApArticleConfig::getIsDown,isDown));
    }

    /**
     * 修改评论状态接口
     *
     * @param message
     * @return void
     * @Description
     * @Author YangJiangPing
     * @Date 10:10 2023/6/29
     * @Param [message]
     */
    @Override
    public void updateComment(String message) {
        // 1: 参数校验
        if (StringUtils.isBlank(message)){
            throw new NullPointerException("wemediaconfig实现类修改评论状态接口发生错误原因是,message为:"+message);
        }
        CommentStatusManageDto commentStatusManageDto = JSON.parseObject(message, CommentStatusManageDto.class);

        // 2:  修改评论状态
        update(Wrappers.<ApArticleConfig>lambdaUpdate()
                .eq(ApArticleConfig::getArticleId,commentStatusManageDto.getArticleId())
                .set(ApArticleConfig::getIsComment,commentStatusManageDto.getOperation()));
        // 3:  清空该文章评论数量
        apArticleMapper.update(new ApArticle(),Wrappers.<ApArticle>lambdaUpdate().eq(ApArticle::getId,commentStatusManageDto.getArticleId())
                .set(ApArticle::getComment,0));
    }
}
